package com.liangjian.mylib.utils;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

public class LocationUtils implements AMapLocationListener {

    private static volatile LocationUtils instance;
    private AMapLocationClient mlocationClient = null;//定位类

    private String NOTIFICATION_CHANNEL_NAME = "";

    private NotificationManager notificationManager = null;

    boolean isCreateChannel = false;

    private Context context;

    private LocationChangeCallback locationChangeCallback;

    private LocationUtils() {

    }

    public static LocationUtils getInstance() {
        if (instance == null) {
            synchronized (LocationUtils.class) {
                if (instance == null) {
                    instance = new LocationUtils();
                }
            }
        }
        return instance;
    }

    //初始化定位
    private void initLocation() {
        //初始化定位
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(context.getApplicationContext());
            //设置定位回调监听
            mlocationClient.setLocationListener(this);
            //设置定位参数
            mlocationClient.setLocationOption(getDefaultOption());
            //启动后台定位，第一个参数为通知栏ID，建议整个APP使用一个
            mlocationClient.enableBackgroundLocation(2001, buildNotification());
        }

    }

    public LocationUtils build(Context context) {
        this.context = context;
        NOTIFICATION_CHANNEL_NAME = MyUtils.getAppName(context);
        initLocation();
        return instance;
    }


    //开始定位
    public LocationUtils startLocation() {
        //启动定位
        if (!mlocationClient.isStarted()) {

            mlocationClient.startLocation();
        }
        return instance;
    }

    //结束定位
    public LocationUtils stopLocation() {
        if (mlocationClient != null) {
            //关闭后台定位，参数为true时会移除通知栏，为false时不会移除通知栏，但是可以手动移除
            mlocationClient.disableBackgroundLocation(true);
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
            mlocationClient = null;
        }
        return instance;
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                locationChangeCallback.onLocationChanged(aMapLocation);
            } else {
                locationChangeCallback.onLocationError(aMapLocation);
            }
        } else {
            locationChangeCallback.onLocationError(aMapLocation);
        }
    }

    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(true);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(3000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        return mOption;
    }

    @SuppressLint("NewApi")
    private Notification buildNotification() {

        Notification.Builder builder = null;
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= 26) {
            //Android O上对Notification进行了修改，如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
            if (null == notificationManager) {
                notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            }
            String channelId = context.getPackageName();
            if (!isCreateChannel) {
                NotificationChannel notificationChannel = new NotificationChannel(channelId,
                        NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
                notificationChannel.enableLights(true);//是否在桌面icon右上角展示小圆点
                notificationChannel.setLightColor(Color.BLUE); //小圆点颜色
                notificationChannel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
                notificationManager.createNotificationChannel(notificationChannel);
                isCreateChannel = true;
            }
            builder = new Notification.Builder(context.getApplicationContext(), channelId);
        } else {
            builder = new Notification.Builder(context.getApplicationContext());
        }
        builder.setContentTitle(MyUtils.getAppName(context))
                .setContentText("正在后台运行")
                .setWhen(System.currentTimeMillis());

//        .setSmallIcon(R.drawable.logo)

        if (android.os.Build.VERSION.SDK_INT >= 16) {
            notification = builder.build();
        } else {
            return builder.getNotification();
        }
        return notification;
    }

    public interface LocationChangeCallback {
        void onLocationChanged(AMapLocation aMapLocation);

        void onLocationError(AMapLocation aMapLocation);
    }

    /**
     * 设置回调
     *
     * @param locationChangeCallback
     * @return
     */
    public LocationUtils setLocationChangeCallback(LocationChangeCallback locationChangeCallback) {
        this.locationChangeCallback = locationChangeCallback;
        return instance;
    }

}
